<template>
  <div class="rework_process">
    <!--处理任务-->
    <mes-search-comp
      ref="mesSearch"
      :searchList="tableColConfig"
      :searchResult="searchResult"
      @searchInfo="search"
    ></mes-search-comp>
    <div class="table_container">
      <el-table
        ref="tableNode"
        border
        stripe
        v-loading="isLoading"
        :data="listData.list"
        :max-height="tableMaxHeight"
        :row-class-name="({row}) => !selectItems.some((item) => item.id === row.id) || 'line_selected'"
        @row-click="row => $refs.tableNode.toggleRowSelection(row)"
        @sort-change="handleSortChange"
        @selection-change="handleSelectionChange"
        @filter-change="handleFilterChange">
        <el-table-column
          width="40"
          type="selection"
          align="center">
        </el-table-column>
        <mes-col-comp :key="column.prop" v-for="column in tableColConfig" :column="column"></mes-col-comp>
        <el-table-column
          fixed="right"
          :resizable="false"
          label="操作"
          min-width="160"
          show-overflow-tooltip
          align="center">
          <template slot-scope="scope">
            <span class="jump_item is_disabled" v-if="scope.row.turnoverProcessStatusValue !== 'InTechProc'">
              编辑工艺
            </span>
            <span class="jump_item" v-else @click.stop="editProcess(scope.row)">
              编辑工艺
            </span>
            <span class="jump_item" @click.stop="showLog(scope.row)">操作记录</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <mes-pagination
      @updateData="init"
      :currentPage="listData.pageNo"
      :total="listData.count || 0"
      :pageSize="listData.pageSize">
    </mes-pagination>
    <!-- 日志详情 -->
    <log-detail ref="logDetail"></log-detail>
  </div>
</template>

<script>
import reworkColumn from './reworkColumn'

export default {
  name: 'ReworkProcess',
  components: {},
  props: {
    judge: {}
  },
  data() {
    return {
      tableColConfig: reworkColumn, // 表格列配置
      isLoading: false, // 数据加载中
      searchData: {}, // 查询参数
      sortData: {}, // 排序对象
      selectItems: [], // 选中的数据
      searchResult: NaN, // 搜索结果数
    }
  },
  mounted() {
    this.init()
  },
  computed: {
    // 列表数据
    listData() {
      return this.$store.state.temporaryWorkbench.reworkListData || {}
    }
  },
  methods: {
    // 列筛选回调
    handleFilterChange(filters) {
      let query = this.$filterParams(this.tableColConfig, filters)
      Object.assign(this.searchData, query.search)
      this.$refs.tableNode.clearSort()
      this.sortData = query.order
      this.init()
    },
    // 初始化方法
    init(page) {
      this.isLoading = true
      let pageObj = page || {
        pageNo: 1,
        pageSize: this.listData.pageSize || 15
      }
      let postData = {
        method: 'post',
        url: 'a/tmpturnoverbatch/tmpTurnoverBatch/list',
        // url: 'a/tmpturnoverbatch/tmpTurnoverBatch/listWishSortInTmpTechProc',
        data: {
          pageNo: pageObj.pageNo,
          pageSize: pageObj.pageSize,
          query: this.searchData,
          sort: this.sortData
        },
        module: 'temporaryWorkbench',
        state: 'reworkListData'
      }
      this.$store.dispatch('saveData', postData).then(res => {
        if (Object.keys(this.searchData).length > 0) {
          this.searchResult = res.count
        } else {
          this.searchResult = NaN
        }
      }).finally(() => {
        this.isLoading = false
      })
    },
    // 搜索方法
    search(params) {
      this.searchData = params
      this.$refs.tableNode.clearFilter()
      this.$refs.tableNode.clearSort()
      this.sortData = {}
      this.init()
    },
    // 刷新方法
    refresh() {
      this.searchData = {} // 查询参数
      this.sortData = {} // 排序对象
      this.$refs.mesSearch.clean()
      this.$refs.tableNode.clearSort()
      this.$refs.tableNode.clearFilter()
      this.init()
    },
    // 排序方法
    handleSortChange(item) {
      this.sortData = this.$getSortOrder(item)
      this.init()
    },
    // 选中方法
    handleSelectionChange(val) {
      this.selectItems = val
      if (this.selectItems.some(item => item.handleStatusValue !== 'Unprocessed')) {
        this.$emit('update:judge', {
          msg: '请选择待处理的数据',
          disabled: true
        })
      } else if (!this.selectItems.length) {
        this.$emit('update:judge', {msg: '请选择需要操作的批次！'})
      } else {
        this.$emit('update:judge', {msg: false})
      }
    },
    editProcess(row) {
      if (row.turnoverProcessStatusValue === 'InTechProc') {
        this.$router.push({
          path: '/reworkWorkStageEdit',
          query: {
            id: row.id,
            turnoverId: row.turnoverId,
          }
        })
      }
    },
    // 日志
    showLog(row) {
      this.$refs.logDetail.openDialog({
        title: '周转码',
        name: row.turnoverBatchNumber,
        url: 'a/turnoverstatuschangehistory/turnoverStatusChangeHistory/list',
        query: {
          turnoverBatchId: row.turnoverId
        }
      })
    },
  }
}
</script>

<style scoped lang="less">
</style>
